package com.kusch;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * https://baomidou.com/pages/981406/#%E5%8F%AF%E9%80%89%E9%85%8D%E7%BD%AE
 *
 * @author Zhangzp
 * @date 2022年08月12日 11:11
 */
public class MpQuickGenerator {
    public static void main(String[] args) {

        String url = "*******";
        String username = "**********";
        String password = "**************";

        //oracle 填这个
        String schema = "";


        DataSourceConfig.Builder dbBuilder
                = new DataSourceConfig.Builder(url, username, password);
        if (StringUtils.isNotBlank(schema)) {
            dbBuilder.schema(schema);
        }
        FastAutoGenerator
                .create(
                        dbBuilder
                )
                .globalConfig(builder -> {
                    builder
                            .outputDir("/Users/kusch/Documents/myProjects/back/generate-code/src/main/java")
                            .author("Mr.Zhangzp")
                            //                .enableSwagger()
                            .dateType(DateType.ONLY_DATE)
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            //关闭生成完成后打开文件夹
                            .disableOpenDir();
                })
                .packageConfig(builder -> {
                    builder
                            .parent("com.goodcol.muses")
                            //模块包名字
                            .moduleName("upms")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            //.xml("mapper.xml")
                            .controller("controller")
                            .other("dto")
                            //这里定义xml输出到 /resources/mapper/xml/ares 文件夹下
                            .pathInfo(Collections.singletonMap(OutputFile.xml,
                                    "/Users/kusch/Documents/myProjects/back/generate-code/src/main/resources/mapper/"));


                })
                .templateConfig(builder -> {
                    builder
                            .disable()
                            .entity("/mp/entity.java")
                            .service("/mp/service.java")
                            .serviceImpl("/mp/serviceImpl.java")
                            .mapper("/mp/mapper.java")
                            .xml("/mp/mapper.xml")
                            .controller("/mp/controller.java");
                })
                .strategyConfig(builder -> {
                    builder
                            //开启跳过视图
                            .enableSkipView()
                            .disableSqlFilter()
                            //.likeTable(new LikeTable("SYS_ANNOUNCEMENT"))
                            .addInclude("zzp_test_tx")

                            .entityBuilder()
                            .enableLombok()
                            //.enableChainModel()
                            .enableTableFieldAnnotation()
                            .fileOverride()

                            .controllerBuilder()
                            .enableRestStyle()

                            .mapperBuilder();
                    //加上@Mapper注解
                    //.enableMapperAnnotation()
                })
                .injectionConfig(consumer -> {
                    Map<String, String> customFile = new HashMap<>();
                    // DTO.java 是后缀，，例如你的实体名叫 AAA   那么你生成的DTO就叫  AAADTO.java
                    customFile.put("QueryDTO.java", "/mp/entityDTOQuery.java.ftl");
                    customFile.put("AddDTO.java", "/mp/entityDTOAdd.java.ftl");
                    customFile.put("UpdateDTO.java", "/mp/entityDTOUpdate.java.ftl");
                    customFile.put("DeleteDTO.java", "/mp/entityDTODelete.java.ftl");

                    //VO
                    customFile.put("QueryVO.java", "/mp/entityVOQuery.java.ftl");
                    customFile.put("PageVO.java", "/mp/entityVOPage.java.ftl");

                    consumer.customFile(customFile);
                })
                .templateEngine(new EnhanceFreemarkerTemplateEngine())
                .execute();
    }
}
